<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开始日期" prop="weekStartDate">
        <el-date-picker clearable v-model="queryParams.weekStartDate" type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="weekEndDate">
        <el-date-picker clearable v-model="queryParams.weekEndDate" type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="check_item" v-for="(item, ix) in teamList" :key="item.id">
        <el-col :span="1.5">
          <el-button type="success" class="royal-blue-button" style="width: 120px;margin-bottom: 10px;" plain
            icon="el-icon-edit" size="mini" @click="handlePlan(item.deptId)"
            v-hasPermi="['factory:factorywplan:edit']">{{ item.deptName }}</el-button>
        </el-col>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="wplandetailList" :row-class-name="tableRowClassName">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="日期" align="center" prop="planDate" width="130px" />
      <el-table-column label="排产情况" align="center" prop="planDesc" />
      <!--<el-table-column label="制造公司编码" align="center" prop="factoryCode" />-->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listFactorywplan, getFactorywplan, delFactorywplan, addFactorywplan, updateFactorywplan } from "@/api/sm/factory/factorywplan";
import { listTeam } from "@/api/system/dept";
import { listWplandetailList } from "@/api/sm/factory/wplandetail";

export default {
  name: "Factorywplan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分厂周排产表格数据
      wplandetailList: [],
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        teamId: null,
      },
      // 查询参数
      queryParams1: {
        level: 3,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getTeamList();
    this.getThisSaturday();
    this.getNextSaturday();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      let temDate = new Date(row.orderDate)
      let siteDate = new Date()

      if (temDate < siteDate) {
        return 'blockRed';
      } else {
        return '';
      }
    },
    getThisSaturday() {
      var currentDate = new Date();
      var currentDay = currentDate.getDay(); // 获取当前是星期几（0-6，0代表星期日）
      var daysUntilFriday = currentDay - 7;
      currentDate.setDate(currentDate.getDate() + daysUntilFriday);
      return this.queryParams.weekStartDate = currentDate;
    },
    getNextSaturday() {
      var currentDate = new Date();
      var currentDay = currentDate.getDay(); // 获取当前是星期几（0-6，0代表星期日）
      var daysUntilFriday = 5 - currentDay;
      currentDate.setDate(currentDate.getDate() + daysUntilFriday);
      return this.queryParams.weekEndDate = currentDate;

      // var currentDate = new Date();
      // var currentDay = currentDate.getDay(); // 获取当前是星期几（0-6，0代表星期日）
      // var daysUntilFriday = 13 - currentDay;
      // currentDate.setDate(currentDate.getDate() + daysUntilFriday);
      // return this.queryParams.weekEndDate=currentDate;
    },
    /** 查询班组列表 */
    getTeamList() {
      this.loading = true;
      listTeam(this.queryParams1).then(response => {
        this.teamList = response.data;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handlePlan(deptId) {
      this.loading = true;
      this.queryParams.teamId = deptId;
      listWplandetailList(this.queryParams).then(response => {
        this.wplandetailList = response.data;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFactorywplan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分厂周排产";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFactorywplan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactorywplan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分厂周排产编号为"' + ids + '"的数据项？').then(function () {
        return delFactorywplan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('factory/factorywplan/export', {
        ...this.queryParams
      }, `factorywplan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.royal-blue-button {
  background-color: #673AB7;
  /* 紫色的十六进制代码 */
  border-color: #673AB7;
  color: #673AB7;
}

.button-row {
  display: flex;
  justify-content: center;
  align-items: center;
}

.button-row button {
  margin-right: 8px;
  /* 按钮之间的间隔 */
}
</style>
